module.exports =
/******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ 		}
/******/ 	};
/******/
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/
/******/ 	// create a fake namespace object
/******/ 	// mode & 1: value is a module id, require it
/******/ 	// mode & 2: merge all properties of value into the ns
/******/ 	// mode & 4: return value when already ns object
/******/ 	// mode & 8|1: behave like require
/******/ 	__webpack_require__.t = function(value, mode) {
/******/ 		if(mode & 1) value = __webpack_require__(value);
/******/ 		if(mode & 8) return value;
/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ 		var ns = Object.create(null);
/******/ 		__webpack_require__.r(ns);
/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ 		return ns;
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


var _require = __webpack_require__(1),
    transformRpx = _require.transformRpx,
    debounce = _require.debounce,
    throttle = _require.throttle,
    sum = _require.sum,
    watch = _require.watch;

var imgHeight = transformRpx(248);

var startX = 0; // touch 事件起始 x 坐标
var startY = 0; // touch 事件起始 x 坐标
var moveStartX = 0; // move 事件起始 x 坐标
var interval = [-1, 0, 1]; // 冗余

// 当前图片状态
var curItem = {
  x: 0,
  y: 0,
  scale: 1
};

Component({
  properties: {
    list: {
      type: Array,
      value: []
    },
    option: {
      type: String,
      value: 'normal'
    },
    likeTitle: {
      type: String,
      value: ''
    }
  },
  data: {
    data: [],
    itemIndex: 1, // 大图预览计数
    // translateX: 0,
    previewData: [], // 预览内容
    moveAnimation: null, // 滚动动画
    lastTapTime: 0, // 记录双击时间
    curIndex: 0, // 当前 index
    initScale: true, // 是否重置缩放
    animationData: {},
    disabled: false,
    model: ''
  },
  translateX: 0,
  ready: function ready() {
    var _this = this;

    // 加载列表
    var _properties = this.properties,
        list = _properties.list,
        likeTitle = _properties.likeTitle;

    this.setData({
      data: list,
      title: likeTitle
    });

    this.initAlbum();

    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear'
    });
    var moveAnimation = wx.createAnimation({
      duration: 0,
      timingFunction: 'linear'
    });
    this.animation = animation;
    this.moveAnimation = moveAnimation;

    wx.getSystemInfo({
      success: function success(res) {
        var model = res.model.substring(0, res.model.indexOf('X')) + 'X';
        if (model === 'iPhone X') {
          _this.setData({
            model: 'iphoneX'
          });
        }
      }
    });

    watch(this, {
      // option 变化重置照片选中状态
      option: function option(newVal) {
        if (newVal === 'normal') {
          var data = _this.data.data;

          data.map(function (item) {
            item.check = false;
            return true;
          });
          _this.setData({
            data: data
          });
        }
      },
      list: function list(newVal) {
        _this.setData({
          data: newVal
        });
        _this.initAlbum();
      },
      likeTitle: function likeTitle(newVal) {
        _this.setData({
          title: newVal
        });
      }
    });
  },

  methods: {
    initAlbum: function initAlbum() {
      this.changeItem();
      this.scrollTop = 0; // 照片墙滚动高度
      this.setData({
        scrollTop: 0
      });
    },

    // 双击还原缩放
    doubleClick: function doubleClick(e) {
      var timeStamp = e.timeStamp;
      var dataset = e.currentTarget.dataset;

      if (timeStamp - dataset.time > 0) {
        if (timeStamp - dataset.time < 300) {
          this.setData({
            initScale: true
          });
        }
      }
      this.setData({
        lastTapTime: timeStamp
      });
    },
    touchstart: function touchstart(e) {
      if (e.touches.length === 1 && !this.moving) {
        var _e$touches$ = e.touches[0],
            pageX = _e$touches$.pageX,
            pageY = _e$touches$.pageY;

        startX = pageX;
        startY = pageY;
        moveStartX = pageX;
        this.moving = true;
      }
    },

    /**
     * 校验 touch 类型
     * @param {*} startX 起始位置 x
     * @param {*} startY 起始位置 y
     * @param {*} pageX 结束位置 x
     * @param {*} pageY 结束位置 y
     * @returns
     */
    touchVerify: function touchVerify(startX, startY, pageX, pageY) {
      if (!startX) {
        return false;
      }

      if (Math.abs(startY - pageY) > Math.abs(startX - pageX)) {
        return false;
      }

      return true;
    },
    touchend: function touchend(e) {
      var _this2 = this;

      var _e$changedTouches$ = e.changedTouches[0],
          pageX = _e$changedTouches$.pageX,
          pageY = _e$changedTouches$.pageY;
      var dataset = e.currentTarget.dataset;

      var curIndex = dataset.index;
      var itemIndex = this.data.itemIndex;
      var translateX = this.translateX;


      if (this.scale) {
        setTimeout(function () {
          _this2.scale = false;
        }, 500);

        this.moving = false;
        return false;
      }

      if (!this.touchVerify(startX, startY, pageX, pageY)) {
        // 处理重复拖动
        if (translateX % transformRpx(750) !== 0) {
          this.animation.translateX(-1 * itemIndex * transformRpx(750) + transformRpx(750)).step();
          this.setData({
            animationData: this.animation.export()
          });
        }

        this.moving = false;
        return false;
      }

      var delta = transformRpx(750) - transformRpx(750) * curItem.scale;
      if (curItem.x > delta + 15 && curItem.x < 0) {
        this.moving = false;
        return false;
      }

      if (startX < pageX && pageX - startX > 50) {
        // 右滑-显示上一张
        this.rightScroll(curIndex);
      } else if (startX > pageX && startX - pageX > 50) {
        // 左滑-显示下一张
        this.leftScroll(curIndex);
      } else {
        this.animation.translateX(-1 * itemIndex * transformRpx(750) + transformRpx(750)).step();
        this.setData({
          animationData: this.animation.export()
        });
        this.moving = false;
      }

      startX = 0;
      moveStartX = 0;
      return true;
    },

    /**
     * 左滑-显示下一张
     * @param {*} curIndex 当前 swiper-item 下标
     * @returns
     */
    leftScroll: function leftScroll(curIndex) {
      var _this3 = this;

      var _data = this.data,
          data = _data.data,
          previewData = _data.previewData;
      var itemIndex = this.data.itemIndex;


      var delta = transformRpx(750) - transformRpx(750) * curItem.scale;
      if (curItem.x > delta + 15) {
        return false;
      }

      if (itemIndex < data.length) {
        this.animation.translateX(-1 * itemIndex * transformRpx(750)).step();

        this.setData({
          previewData: previewData,
          itemIndex: itemIndex + 1,
          animationData: this.animation.export()
        });

        this.translateX = -1 * itemIndex * transformRpx(750);

        setTimeout(function () {
          if (itemIndex > 1) {
            if (data[itemIndex + 1]) {
              var transformIndex = curIndex ? (curIndex - 1) % 3 : 2;
              var deltaX = previewData[transformIndex].translateX + 3 * transformRpx(750);
              previewData[transformIndex] = data[itemIndex + 1];
              previewData[transformIndex].translateX = deltaX;
            }
          }

          _this3.setData({
            previewData: previewData,
            initScale: true,
            disabled: false
          });
        }, 200);

        curItem.scale = 0;
        curItem.x = 0;
        curItem.y = 0;
        this.moving = false;
      } else {
        this.moving = false;
      }

      return true;
    },

    /**
     * 右滑-显示上一张
     * @param {*} curIndex 当前 swiper-item 下标
     * @returns
     */
    rightScroll: function rightScroll(curIndex) {
      var _this4 = this;

      var _data2 = this.data,
          previewData = _data2.previewData,
          data = _data2.data;
      var itemIndex = this.data.itemIndex;

      if (curItem.scale > 1) {
        if (curItem.x < 0) {
          return false;
        }
      }

      if (itemIndex > 1) {
        this.animation.translateX(-1 * itemIndex * transformRpx(750) + 2 * transformRpx(750)).step();

        this.setData({
          animationData: this.animation.export(),
          itemIndex: itemIndex - 1,
          animation: false
        });

        this.translateX = -1 * itemIndex * transformRpx(750) + 2 * transformRpx(750);

        setTimeout(function () {
          if (itemIndex < data.length) {
            if (data[itemIndex - 3]) {
              var transformIndex = curIndex === 2 ? 0 : curIndex + 1;
              var deltaX = previewData[transformIndex].translateX - 3 * transformRpx(750);
              previewData[transformIndex] = data[itemIndex - 3];
              previewData[transformIndex].translateX = deltaX;
            }
          }

          _this4.setData({
            previewData: previewData,
            initScale: true,
            disabled: false
          });

          _this4.moving = false;
        }, 200);
      } else {
        this.moving = false;
      }

      return true;
    },
    moveItem: function moveItem(e) {
      this.moveEvent = e;
      this.throttleTouchmove();
    },

    throttleTouchmove: throttle(function () {
      this.touchmove(this.moveEvent);
    }, 10),
    touchmove: function touchmove(e) {
      var _e$changedTouches$2 = e.changedTouches[0],
          pageX = _e$changedTouches$2.pageX,
          pageY = _e$changedTouches$2.pageY;


      if (this.scale) {
        return false;
      }

      if (!moveStartX) {
        return false;
      }

      if (!this.touchVerify(startX, startY, pageX, pageY)) {
        return false;
      }

      if (e.touches.length > 1) {
        return false;
      }

      var delta = transformRpx(750) - transformRpx(750) * curItem.scale;
      if (curItem.x > delta + 15 && curItem.x < 0) {
        return false;
      }

      if (moveStartX < pageX) {
        // 右滑
        this.moveRight(e);
      } else if (moveStartX > pageX) {
        // 左滑
        this.moveLeft(e);
      }

      moveStartX = pageX;
      return true;
    },

    /**
     * 向左滑动
     * @param {*} e
     * @returns
     */
    moveLeft: function moveLeft(e) {
      var pageX = e.touches[0].pageX;
      var translateX = this.translateX;
      var _data3 = this.data,
          itemIndex = _data3.itemIndex,
          data = _data3.data;


      var delta = transformRpx(750) - transformRpx(750) * curItem.scale;
      if (curItem.x > delta + 15) {
        return false;
      }

      if (itemIndex < data.length) {
        this.moveAnimation.translateX(translateX - (moveStartX - pageX)).step();
        this.setData({
          animationData: this.moveAnimation.export(),
          disabled: true
        });

        this.translateX = translateX - (moveStartX - pageX);

        this.triggerEvent('updateitem', {
          curItem: data[itemIndex]
        });
      }

      return true;
    },

    /**
     * 向右滑动
     * @param {*} e
     * @returns
     */
    moveRight: function moveRight(e) {
      var pageX = e.touches[0].pageX;
      var translateX = this.translateX;
      var _data4 = this.data,
          itemIndex = _data4.itemIndex,
          data = _data4.data;


      if (curItem.scale > 1) {
        if (curItem.x < 0) {
          return false;
        }
      }

      if (itemIndex > 1) {
        this.moveAnimation.translateX(translateX - (moveStartX - pageX)).step();
        this.setData({
          animationData: this.moveAnimation.export(),
          disabled: true
        });

        this.translateX = translateX - (moveStartX - pageX);

        this.triggerEvent('updateitem', {
          curItem: data[itemIndex - 2]
        });
      }

      return true;
    },

    /**
     * 标记缩放
     * @param {*} e
     */
    onScale: function onScale(e) {
      var detail = e.detail;

      curItem.scale = detail.scale;
      curItem.x = detail.x;
      curItem.y = detail.y;
      this.scale = true;
    },

    /**
     * 记录移动位置
     * @param {*} e
     */
    onChange: function onChange(e) {
      var detail = e.detail;

      curItem.x = detail.x;
      curItem.y = detail.y;
    },

    /**
     * 照片墙滚动
     * @param {*} e
     */
    scroll: function scroll(e) {
      this.scrollTop = e.detail.scrollTop;
      this.changeItem();
    },

    changeItem: debounce(function () {
      var data = this.data.data;

      var minConlum = (Math.floor(this.scrollTop / imgHeight) - 2) * 3;
      var maxConlum = minConlum + 8 * 3;
      data.map(function (item, index) {
        if (index >= minConlum && index < maxConlum) {
          item.show = true;
        }
        return true;
      });

      this.setData({
        data: data
      });
    }, 500),
    /**
     * 预览大图
     * @param {*} e
     * @returns
     */
    preview: function preview(e) {
      var _this5 = this;

      var data = this.data.data;
      var url = e.currentTarget.dataset.url;
      var type = e.currentTarget.dataset.type;

      var index = data.findIndex(function (value) {
        return value.src === url;
      });
      var previewData = [];

      if (type === 'download') {
        var downloadList = [];
        data.map(function (item) {
          if (item.check) {
            downloadList.push(item.src);
          }
          return true;
        });

        if (downloadList.length >= 9 && !data[index].check) {
          wx.showToast({
            title: '最多只能同时选择 9 张照片~',
            icon: 'none'
          });
          return false;
        }

        data[index].check = !data[index].check;
        this.setData({
          data: data
        });
        return false;
      }

      var _interval = interval;
      if (index === data.length - 1) {
        _interval = [-2, -1, 0];
      } else if (index === 0) {
        _interval = [0, 1, 2];
      }

      _interval.map(function (item) {
        if (data[index + item]) {
          data[index + item].translateX = index * transformRpx(750) + item * transformRpx(750);
          previewData.push(data[index + item]);
        }
        return true;
      });

      this.animation.translateX(-1 * index * transformRpx(750)).step();

      this.setData({
        previewData: previewData,
        animationData: this.animation.export(),
        initScale: true,
        previewShow: true,
        itemIndex: index + 1
      });

      this.translateX = -1 * index * transformRpx(750);

      this.triggerEvent('updateitem', {
        curItem: data[index]
      });
      this.clickLike = false;

      setTimeout(function () {
        _this5.setData({
          animation: true
        });
      }, 200);

      return true;
    },

    /**
     * 关闭大图预览
     */
    close: function close() {
      this.setData({
        previewShow: false,
        animation: false
      });

      this.triggerEvent('close', {
        click: this.clickLike
      });
    },

    /**
     * 下载
     * @returns
     */
    download: function download() {
      var _this6 = this;

      if (this.downloading) {
        return false;
      }
      var data = this.data.data;

      var downloadList = [];
      var progress = [];
      data.map(function (item) {
        if (item.check) {
          downloadList.push(item.src);
          progress.push(0);
        }
        return true;
      });

      if (downloadList.length === 0) {
        wx.showToast({
          title: '请选择图片',
          icon: 'none'
        });
        return false;
      }

      var progressThrottle = throttle(function () {
        var all = downloadList.length * 100;
        var _progress = parseInt(sum(progress) / all * 100, 10);
        var _progressContent = _progress > 0 ? '\u4E0B\u8F7D\u4E2D ' + _progress + '%' : '下载中...';
        wx.showToast({
          title: _progressContent,
          icon: 'none'
        });

        if (progress >= 100) {
          _this6.downloading = false;
        }
      }, 100);

      wx.authorize({
        scope: 'scope.writePhotosAlbum',
        success: function success() {
          _this6.triggerEvent('finish');
          _this6.downloading = true;
          downloadList.map(function (item, index) {
            wx.downloadFile({
              url: item,
              success: function success(res) {
                _this6.downloading = false;
                wx.saveImageToPhotosAlbum({
                  filePath: res.tempFilePath,
                  success: function success() {
                    var all = downloadList.length * 100;
                    var _progress = parseInt(sum(progress) / all * 100, 10);
                    if (_progress >= 100) {
                      setTimeout(function () {
                        wx.showToast({
                          title: '下载成功',
                          icon: 'success',
                          duration: 1500
                        });
                        _this6.triggerEvent('download', {
                          downloadList: downloadList
                        });
                      }, 500);
                    }
                  }
                });
              },
              fail: function fail(err) {
                _this6.downloading = false;
                var res = err && err.errMsg ? err.errMsg : '下载失败！';
                wx.showToast({
                  title: res,
                  icon: 'none',
                  duration: 2000
                });
              }
            }).onProgressUpdate(function (res) {
              progress[index] = res.progress;
              progressThrottle();
            });
            return true;
          });
        },
        fail: function fail() {
          _this6.downloading = false;
          wx.showToast({
            title: '请授权后,重新下载！',
            icon: 'none',
            duration: 2000
          });
          wx.openSetting({});
        }
      });

      return true;
    },

    /**
     * 单图下载
     */
    downloadCur: function downloadCur() {
      var _this7 = this;

      if (this.downloading) {
        return false;
      }

      var _data5 = this.data,
          itemIndex = _data5.itemIndex,
          data = _data5.data;

      var progress = 0;

      var progressThrottle = throttle(function () {
        var _progressContent = progress > 0 ? '\u4E0B\u8F7D\u4E2D ' + progress + '%' : '下载中...';
        wx.showToast({
          title: _progressContent,
          icon: 'none'
        });

        if (progress >= 100) {
          _this7.downloading = false;
        }
      }, 100);

      wx.authorize({
        scope: 'scope.writePhotosAlbum',
        success: function success() {
          _this7.downloading = true;
          wx.downloadFile({
            url: data[itemIndex - 1].src,
            success: function success(res) {
              _this7.downloading = false;
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function success() {
                  wx.showToast({
                    title: '下载成功',
                    duration: 1500
                  });

                  _this7.triggerEvent('download', {
                    downloadList: [data[itemIndex - 1].src]
                  });
                }
              });
            },
            fail: function fail(err) {
              _this7.downloading = false;
              var res = err && err.errMsg ? err.errMsg : '下载失败！';
              wx.showToast({
                title: res,
                icon: 'none',
                duration: 2000
              });
            }
          }).onProgressUpdate(function (res) {
            progress = res.progress;
            progressThrottle();
          });
        },
        fail: function fail() {
          this.downloading = false;
          wx.showToast({
            title: '请授权后,重新保存！',
            icon: 'none',
            duration: 2000
          });
          wx.openSetting({});
        }
      });
      return true;
    },

    /**
     * 收藏
     */
    like: function like(e) {
      var _data6 = this.data,
          itemIndex = _data6.itemIndex,
          data = _data6.data;
      var dataset = e.currentTarget.dataset;


      this.triggerEvent('like', {
        img: data[itemIndex - 1],
        title: dataset.title
      });

      this.clickLike = true;
    },

    /**
     * 页面点击
     */
    clickSwiper: function clickSwiper() {
      this.triggerEvent('clickswiper');
    }
  }
});

/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";


// rpx 换算
function transformRpx(rpx) {
  var systemInfo = wx.getSystemInfoSync();
  var px = rpx / 750 * systemInfo.windowWidth;
  return px;
}

// 防抖
function debounce(method, time) {
  var timer = null;
  return function () {
    var context = this;
    clearTimeout(timer);
    timer = setTimeout(function () {
      method.call(context);
    }, time);
  };
}

// 节流
function throttle(func, delay) {
  var prev = Date.now();
  return function () {
    var context = this;
    var now = Date.now();
    if (now - prev >= delay) {
      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      func.apply.apply(func, [context].concat(args));
      prev = Date.now();
    }
  };
}

function sum(arr) {
  return arr.reduce(function (prev, cur) {
    return prev + cur;
  }, 0);
}

function defineReactive(data, key, val, fn) {
  Object.defineProperty(data, key, {
    configurable: true,
    enumerable: true,
    get: function get() {
      return val;
    },
    set: function set(newVal) {
      if (newVal === val) return;
      if (fn) {
        fn(newVal);
      }
      val = newVal;
    }
  });
}

function watch(ctx, obj) {
  Object.keys(obj).forEach(function (key) {
    defineReactive(ctx.properties, key, ctx.properties[key], function (value) {
      obj[key].call(ctx, value);
    });
  });
}

module.exports = {
  transformRpx: transformRpx,
  debounce: debounce,
  throttle: throttle,
  sum: sum,
  watch: watch
};

/***/ })
/******/ ]);